/*
* @Author: suddev
* @Date:   2017-08-14 19:22:40
* @Last Modified by:   suddev
* @Last Modified time: 2017-08-14 22:09:36
*/

'use strict';

//表单里的错误提示
var formError = {
    show    : function(errorMsg){
        $('.error-item').show().find('.error-message').text(errorMsg);
    },
    hide    : function(){
        $('.error-item').hide().find('.error-message').text('');
    }
};

//page逻辑部分
var page = {
    init : function () {
        this.bindEvent();
    },
    bindEvent : function () {
        var _this = this;
        //登陆按钮的点击
        $('#submit').click(function(){
            _this.submit();
        });
        // 如果按下回车，也执行提交
        $('.form-horizontal').keyup(function(e){
            // keyCode13表示回车
            if (e.keyCode === 13) {
                _this.submit();
            }
        });
    },
    // 提交表单
    submit : function () {
        var formData = {
            realname	: $.trim($('#realname').val()),
            sex 		: $.trim($('#sex:checked').val()),
            birthday 	: $.trim($('#birthday').val()),
            phone 		: $.trim($('#phone').val()),
            cellphone 	: $.trim($('#cellphone').val()),
            address 	: $.trim($('#address').val()),
            startDate 	: $.trim($('#startDate').val()),
            memo 		: $.trim($('#memo').val())
        },
        //表单验证结果
        validateResult = this.formValidate(formData);
        // 验证成功
        if (validateResult.status) {
            //提交
            _info.input_info(formData,function(res){
                //alert("录入成功");
                formError.hide();
                $.gritter.add({
            	// (string | mandatory) the heading of the notification
            	title: '信息',
            	// (string | mandatory) the text inside the notification
           	 	text: '客户信息录入成功',
            	// (string | optional) the image to display on the left
            	image: 'assets/img/ui-sam.jpg',
            	// (bool | optional) if you want it to fade out on its own or just sit there
            	sticky: false,
            	// (int | optional) the time you want it to be alive for before fading out
            	time: '',
            	// (string | optional) the class name you want to apply to that specific message
            	class_name: 'my-sticky-class'
        	});
            },function(errorMsg){
                formError.show(errorMsg);
            });
            $('#reset').click();
        }
        //验证失败
        else{
            // 错误提示
            formError.show(validateResult.msg);
        }
    },
    formValidate : function(formData){
        var result = {
            status  : false,
            msg     : ''
        };
        if (!_tools.validate(formData.realname,'require')) {
            result.msg = '客户姓名不能为空';
            return result;
        }
        if (!_tools.validate(formData.birthday,'require')) {
            result.msg = '客户出生日期不能为空';
            return result;
        }
        var reg = /^(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[13579][26])00))-02-29)$/;
		var regExp = new RegExp(reg);
		if(!regExp.test(formData.birthday)){
　　         result.msg = '客户出生日期格式不正确，例:2012-01-01';	
　　			return result;	
		}
        if (!_tools.validate(formData.phone,'require')) {
            result.msg = '客户联系电话不能为空';
            return result;
        }
		var reg2 = /^((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)$/;
        var regExp2 = new RegExp(reg2);
        if(!regExp2.test(formData.phone)){
　　         result.msg = '客户联系电话格式不正确';	
　　			return result;	
		}
        if (!_tools.validate(formData.cellphone,'require')) {
            result.msg = '客户手机不能为空';
            return result;
        }
        if(!regExp2.test(formData.cellphone)){
　　         result.msg = '客户手机格式不正确';	
　　			return result;	
		}
        if (!_tools.validate(formData.address,'require')) {
            result.msg = '客户详细地址不能为空';
            return result;
        }
        if (!_tools.validate(formData.startDate,'require')) {
            result.msg = '客户加入日期不能为空';
            return result;
        }
        if(!regExp.test(formData.startDate)){
　　         result.msg = '客户加入日期格式不正确，例:2012-01-01';	
　　			return result;	
		}
        //通过验证，返回正确提示
        result.status = true;
        result.msg    = '验证通过';
        return result;
    }
}
$(function () {
   page.init(); 
});